/* * Copyright (C) 2014 Saeed Masoumi & Saeed Rajabzade * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package engine.gameView.soundEffect; import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine.Info; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; import static javax.sound.sampled.AudioSystem.getAudioInputStream; import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED; /** * * @author Saeed Masoumi */ public class OggPlayer { public void play(String filePath) { final File file = new File(filePath); try (final AudioInputStream in = getAudioInputStream(file)) { final AudioFormat outFormat = getOutFormat(in.getFormat()); final Info info = new Info(SourceDataLine.class, outFormat); try (final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) { if (line != null) { line.open(outFormat); line.start(); AudioInputStream inputMystream = AudioSystem.getAudioInputStream(outFormat, in); stream(inputMystream, line); line.drain(); line.stop(); } } } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) { throw new IllegalStateException(e); } } private AudioFormat getOutFormat(AudioFormat inFormat) { final int ch = inFormat.getChannels(); final float rate = inFormat.getSampleRate(); return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false); } private void stream(AudioInputStream in, SourceDataLine line) throws IOException { final byte[] buffer = new byte[4096]; for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) { line.write(buffer, 0, n); } } }